<template>
  <div>
    <el-container>
      <el-header>
        <div class="header">
          <img
            src="../assets/kclogo.png"
            alt=""
            width="40px"
            height="40px"
          />
          <span class="header-title">好好学习element ui框架,努力搬砖，做一个勤奋的搬砖工！</span>
        </div>
        <el-button
          type="info"
          @click="logout"
        >退出 </el-button>
      </el-header>
      <el-container>
        <el-aside :width="toggleStates ? '64px' : '200px'">
          <div
            class="toggle-btn"
            @click="toggle()"
          >
            |||
          </div>

          <el-menu
            background-color="#304156"
            text-color="#fff"
            active-text-color="#fb9099"
            :unique-opened="true"
            :collapse="toggleStates"
            :collapse-transition="false"
            :router="true"
            :default-active="active"
          >
            <el-submenu
              :index="item.id + ''"
              v-for="item in menuList"
              :key="item.id"
            >
              <template slot="title">
                <i :class="icons[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="changeIndex('/' + subItem.path)"
              >
                <template>
                  <i class="el-icon-menu"></i>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  data () {
    return {
      menuList: [],
      icons: {
        125: 'el-icon-location',
        103: 'el-icon-s-fold',
        101: 'el-icon-message-solid',
        102: 'el-icon-s-custom',
        145: 'el-icon-s-home'
      },
      toggleStates: false,
      active: '/'
    }
  },
  created () {
    this.getMenuList()
    this.active = window.sessionStorage.getItem('active')
  },
  methods: {
    //登录
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    //获取菜单列表
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      this.menuList.push({
        "id": 110,
        "authName": "大杂烩管理",
        "path": "map",
        "children": [
          {
            "id": 110,
            "authName": "地图页面",
            "path": "map",
            "children": [

            ],
            "order": null
          },
          {
            "id": 111,
            "authName": "vuex管理",
            "path": "vuex",
            "children": [

            ],
            "order": null
          }
        ],
        "order": 8
      })

    },
    //开关页面
    toggle () {
      this.toggleStates = !this.toggleStates
    },
    //切换页面
    changeIndex (path) {
      window.sessionStorage.setItem('active', path)
      this.active = path
    },
    toMap () {
      this.$router.push('/map')
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  background-color: #304156;
  display: flex;
  flex-direction: row;
  justify-items: center;
  justify-content: space-between;
  padding: 10px;
  color: #fff;
}

.el-aside {
  background-color: #304156;
  height: 937px;

  .toggle-btn {
    background: #4a5064;
    font-size: 10px;
    text-align: center;
    line-height: 24px;
    color: #fff;
    letter-spacing: 0.2em;
    cursor: pointer;
  }

  .el-menu {
    border-right: none;

    .i {
      margin-right: 10px;
    }
  }
}

.header {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.header-title {
  margin-left: 10px;
  font-size: 20px;
}
</style>
